package Offer.offer2020;

/**
 * 二维数组的查找
 */
public class Test03 {
    public static void main(String[] args) {
        int[][] matrix = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
        System.out.println(isContainsNum(matrix, 15));
    }

    public static boolean isContainsNum(int[][] matrix, int num) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }

        int rows = matrix.length;
        int columns = matrix[0].length;

        int row = 0;
        int column = columns - 1;
        while (row <= rows - 1 && column >= 0) {
            if (num == matrix[row][column]) {
                return true;
            } else if (num < matrix[row][column]) {
                column--;
            } else {
                row++;
            }
        }
        return false;
    }



}
